<?php

namespace App\Http\Controllers\Store;

use App\Model\Leave;
use App\Model\Sch_desc;
use App\Model\Scheduling;
use App\Model\Settle_desc;
use App\Model\Settlement;
use App\Model\Staff_task;
use App\Model\Store;
use App\Model\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

//店铺信息
class Store_infoController extends CommonController
{
    //

	protected $Settlement;
	protected $Settle_desc;
	protected $Staff_task;
	protected $scheduling;
	protected $sch_desc;
	protected $task;
	protected $leave;
	public function __construct()
	{
		parent::__construct();

		$this->scheduling=new Scheduling();
		$this->Settlement=new Settlement();
		$this->sch_desc=new Sch_desc();
		$this->Settle_desc=new Settle_desc();
		$this->Sch_desc=new Sch_desc();
		$this->Staff_task=new Staff_task();
		$this->task=new Task();
		$this->leave=new Leave();
	}


//	店铺信息首页
	public function index(){
		$data['userInfo']=Auth::user();
		$data['store']=Store::where(['id'=>$this->store_id])->first();
		$data['this_month']=date("Y-m",time());
		return $data;
	}


	//店铺结算首页
	public function settle(Request $request){
		$where['store_id']=$this->store_id;
		$where['dataTime']=isset($request->dataTime)?$request->dataTime:date("Y-m",time());
		try{
			$Arr=$this->Settle_desc->station_staff($where);
			$data['desc']=[];
			foreach ($Arr as $kk=>$vv){
				$data['desc'][]=$vv;
			}
			$data['settle_sum']=0;
			foreach($data['desc'] as $k=>$v){
				$data['settle_sum']=$data['settle_sum']+$v['settle_sum'];
			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

	//店铺结算详情
	public function settle_desc(Request $request){
		$where['store_station_id']=$request->store_station_id;
		$where['dateTime']=isset($request->dateTime)?$request->dateTime:date("Y-m",time());
//		return $where;
		try{
			$data=[];$newArr=[];
			 $data=$this->Settle_desc->station_list($where);
			foreach ($data as $k=>$v){
				$newArr[]=$v;
			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($newArr);
		}else{
			return $this->failed($message);
		}
	}

//	店铺任务
	public function store_task(Request $request){
		$where['store_id']=$this->store_id;
		$firstday = date('Y-m-01', strtotime($request->time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['start_time']=$firstday;
		$where['end_time']=$lastday;
		try{
			$data=$this->task->store_task_list($where);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}

	}

//	历史执行人
	public function history_implement(Request $request){
		$time=isset($request->time)?$request->time:date("Y-m",time());
		$where['store_id']=$this->store_id;
		$firstday = date('Y-m-01', strtotime($time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['start_time']=$firstday;
		$where['end_time']=$lastday;
		try{
			$data=$this->Staff_task->history_staff($where);

		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

//	签到记录
	public function sign(Request $request){
		$time=isset($request->time)?$request->time:date("Y-m",time());
//		return $where;
		$where['store_station_id']=isset($request->store_station_id)?$request->store_station_id:null;
		$where['store_id']=$this->store_id;
		$firstday = date('Y-m-01', strtotime($time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['start_time']=$firstday;
		$where['end_time']=$lastday;
		try{
			$data['leavel']=$this->leave->allLeave($where);
			$str=$this->sch_desc->allSign($where);
			$data['sign']=$str*100;
			$data['no_sign']=(1-$str)*100 ;


		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}


//	中止记录
	public function leave_jl(Request $request){
		$where['time']=isset($request->time)?$request->time:date("Y-m",time());
		$where['store_station_id']=$request->store_station_id;
		$where['store_id']=$this->store_id;
		$firstday = date('Y-m-01', strtotime($request->time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['start_time']=$firstday;
		$where['end_time']=$lastday;
		$data=$this->leave->all_leave_data($where);
		foreach($data as $k=>$v){
			$data[$k]['created']=date("Y-m-d",strtotime($v['created_at']));
		}
		return $this->success($data);
	}


//	添加排班
	public function sch_add(Request $request){

		//		模拟数据
		$class_name=date("Ymd",time()).rand(1,999);
		$data=json_decode($request->desc,true);
//		$data=[
//			'time'=>['2018-04-23','2018-04-22'],
//			'status'=>1,
//			'desc'=>[
//				[
//					'station_id'=>1,
//					'staff_id'=>'1',
//					'start_time'=>' 08:00',
//					'end_time'=>'18:00',
//				],
//				[
//					'station_id'=>2,
//					'staff_id'=>'2',
//					'start_time'=>'10:00',
//					'end_time'=>'16:00',
//				]
//			],
//
//		];
//		新增排班
		$data_sch=[
			'class_name'=>$class_name,
			'status'=>$data['status'],
			'company_id'=>$this->company_id($this->store_id),
			'store_id'=>$this->store_id,
		];
		DB::beginTransaction();
		try{
			$id=$this->scheduling->create_sch($data_sch);
			$res=$this->sch_desc->create_sch_desc($id,$data,$this->store_id);
			DB::commit();

		}catch (\Exception $exception){
			DB::rollback();
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->stored(['code'=>201,'messaage'=>'添加成功']);
		}else{
			return $message;$this->failed($message);
		}
	}


//	排班列表
	public function sch_list(Request $request){
		$where['company_id']=$this->company_id($this->store_id);
		$where['store_id']=$this->store_id;
		$where['dataTime']=$request->dataTime;
		$firstday = date('Y-m-01', strtotime($request->dataTime)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		try{
			$where['start_time']=isset($request->dataTime)?$firstday:null;
			$where['end_time']=isset($request->dataTime)?$lastday:null;
			$data=$this->sch_desc->store_sch_lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

//	排班详情
	public function sch_desc(Request $request){
		$where['time']=$request->time;
		$where['store_id']=$request->store_id;
		try{
			$data=$this->sch_desc->store_sch_desc($where);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}



//	根据店铺查询公司
	public function company_id($store_id){
		return Store::where(['id'=>$store_id])->first()->company_id;
	}



}
